package com.hoshiicloud.payment.param;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 下单参数
 */
@ApiModel("下单参数")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class PayRequest implements Serializable {

    @ApiModelProperty(value = "支付渠道,暂时支持weixin", example = "weixin", required = true)
    private String channel;

    @ApiModelProperty(value = "支付金额,单位为分", example = "10", required = true)
    private Integer amount;

    @ApiModelProperty(value = "订单名称", example = "测试订单", required = true)
    private String orderName;

    @ApiModelProperty(value = "商品主类型 :0-虚拟类商品,1-实物类商品", example = "1", required = true)
    private String goodsType;

    @ApiModelProperty(value = "支付模式,暂时支持PAGE,WAP,WEBAPP,NATIVEAPP", example = "PAGE", required = true)
    private String payMode;

    @ApiModelProperty(value = "商户订单号", example = "1234567890", required = true)
    private String merOrderId;

    @ApiModelProperty(value = "商户号", example = "1", required = true)
    private Long merId;

    @ApiModelProperty(value = "绝对超时时间，格式为yyyy-MM-dd HH:mm:ss", example = "2019-06-14 17:34:34")
    private String timeExpire;

    @ApiModelProperty(value = "前端返回地址,HTTP/HTTPS开头字符串")
    private String returnUrl;

    @ApiModelProperty(value = "签名,将除sign外字段以字典升序排列，用key=value加上连接符&的方式拼接，最后追加&key=商户密钥，得到的字符串进行32位md5加密。例:key1=value1&key2=value2&key=secretKey", required = true)
    private String sign;

    @ApiModelProperty(value = "附加数据,回调时原样返回,参数需进行UrlEncode")
    private String attach;

    @ApiModelProperty(value = "用户端IP地址", example = "127.0.0.1", required = true)
    private String userIP;
    @ApiModelProperty(value = "用户前端ID", example = "1", required = true)
    private String userId;

    @ApiModelProperty(value = "用户第三方ID,部分支付模式下必传,如微信小程序/h5支付", example = "2")
    private String userThirdId;

    @ApiModelProperty(value = "支付订单ID", hidden = true)
    private String orderId;

    /**
     * 下单参数转微信支付请求参数
     *
     * @return
     */
    public Map<String, String> WeixinRequestData() {
        Map<String, String> map = new HashMap<>();
        map.put("body", this.orderName);
        //2019.08.20 同时将支付流水的id写入attach
        map.put("attach",orderId);
        map.put("out_trade_no", orderId);
        map.put("total_fee", amount.toString());
        map.put("spbill_create_ip", userIP);
        String tradeType = "";
        switch (payMode) {
            case "PAGE":
                tradeType = "NATIVE";
                break;
            case "WAP":
                tradeType = "MWEB";
                break;
            case "WEBAPP":
                tradeType = "JSAPI";
                map.put("openid", userThirdId);
                break;
            case "NATIVEAPP":
                tradeType = "APP";
                break;
            default:
                break;
        }
        //
        map.put("trade_type", tradeType);
        if (tradeType.equals("NATIVE")) {
            //trade_type=NATIVE时，此参数必传。此参数为二维码中包含的商品ID，此处传入商户订单号
            map.put("product_id", merOrderId);
        }

        return map;
    }

    ;


}
